﻿using HCMS.CommonDtos;
using HCMS.Dtos.OAuth2;
using HCMS.Extensions;
using HCMS.HttpApi.Site.Filter;
using HCMS.HttpApi.Site.Model.OAuth2;
using HCMS.IServices.OAuth2;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;

namespace HCMS.HttpApi.Site.Controllers
{
    /// <summary>
    /// OAuth 2.0 认证授权 控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "oauth2")]
    [ApiController]
    [Route("oauth2")]
    [TypeFilter(typeof(ActionExecutionLogsFilter))]
    [TypeFilter(typeof(ResultExecutionLogsFilter))]
    public class OAuth2Controller : AbpControllerBase
    {
        // 推荐认证方式：https://agents.baidu.com/docs/develop/out-deployment/get_accessionToken/

        private readonly IOAuth2AppAppService oauth2AppAppService;

        public OAuth2Controller(IOAuth2AppAppService oauth2AppAppService)
        {
            this.oauth2AppAppService = oauth2AppAppService;
        }

        /// <summary>
        /// 获取 AccessToken
        /// </summary>
        /// <param name="args">应用参数</param>
        /// <returns></returns>
        [HttpGet("token")]
        [ProducesResponseType(typeof(AppTokenDto), StatusCodes.Status200OK)]
        public async Task<IActionResult> Token([FromQuery] GetAccessTokenArgs args)
        {
            if (args == null)
            {
                throw new UserFriendlyException("参数无效");
            }

            var result = await oauth2AppAppService.GetToken(args.AppId, args.Secret);
            return Ok(new AjaxResultDto() { Result = result });
        }

    }
}
